﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;

/// <summary>
/// RegisterNew 的摘要说明
/// </summary>
public class RegisterNew
{

    //
    // TODO: 在此处添加构造函数逻辑
    //
    static string conn134=getsql.getstr("reg_str2");
    static string conn70 = getsql.getstr("messtr");

    #region 注册
    public static void regsatechange(int stat, string id, string pwd, string myname, int sex, int age, string birth, int pid, int cid, string ip)
    {
        try
        {
            SqlParameter[] p ={
                   SqlHelper.MakeInParam("@UserIdx",SqlDbType.Int,4,stat),
                   SqlHelper.MakeInParam("@UserId",SqlDbType.VarChar,20,id),              // @UserId Varchar(20),
                   SqlHelper.MakeInParam("@MyName",SqlDbType.VarChar,50,myname),          //@MyName Varchar(50),
                   SqlHelper.MakeInParam("@Password",SqlDbType.VarChar,50,pwd),           //@Password Varchar(50),
                   SqlHelper.MakeInParam("@MySex",SqlDbType.Int,4,sex),                   //@MySex Int,
                   SqlHelper.MakeInParam("@MyAge",SqlDbType.Int,4,age),                   //@MyAge Int,
                   SqlHelper.MakeInParam("@MyBirthday",SqlDbType.VarChar,50,birth),       //@MyBirthday Varchar(50),
                   SqlHelper.MakeInParam("@ProvinceId",SqlDbType.Int,4,pid),              //@ProvinceId Int,
                   SqlHelper.MakeInParam("@CityId",SqlDbType.Int,4,cid),                  //@CityId Int,
                   SqlHelper.MakeInParam("@Email",SqlDbType.VarChar,50,""),             //@Email Varchar(50),
                   SqlHelper.MakeInParam("@RegisterIp",SqlDbType.VarChar,50,ip),          //@RegisterIp Varchar(50),
                   SqlHelper.MakeInParam("@Introducer",SqlDbType.VarChar,50,"9158web")        //@Introducer Varchar(50),
            };
            SqlHelper.ExecuteNonQuery(conn134, CommandType.StoredProcedure, "Register_Insert_MemberStat_New", p);//conn.Conn134,
        }
        catch (Exception)
        {

            // Common.Logs.WriteErrorLog(e.Message + "[注册失败134数据库：" + DateTime.Now.ToString() + "]");
        }

    }
    #endregion

    #region 注册 获取用户编号（返回值 useridx）

    public static int regall(string id, string myname, string pwd, int sex, int age, string birth, int pid, int cid, int mcss, string ip, string intro, string regsource, int minidx, int maxidx, int stat)
    {
        try
        {
            SqlParameter[] p =
                {
                   SqlHelper.MakeInParam("@UserId",SqlDbType.VarChar,20,id),                //@UserId Varchar(20),
                   SqlHelper.MakeInParam("@MyName",SqlDbType.VarChar,50,myname),           //@MyName Varchar(50),
                   SqlHelper.MakeInParam("@Password",SqlDbType.VarChar,50,pwd),            //@Password Varchar(50),
                   SqlHelper.MakeInParam("@MySex",SqlDbType.Int,4,sex),                     //@MySex Int,
                   SqlHelper.MakeInParam("@ProvinceId",SqlDbType.Int,4,pid),                //@ProvinceId Int,  
                   SqlHelper.MakeInParam("@CityId",SqlDbType.Int,4,cid),                   //@CityId Int,
                   SqlHelper.MakeInParam("@MyAge",SqlDbType.Int,4,age), //@MyAge Int,
                   SqlHelper.MakeInParam("@Birthday",SqlDbType.VarChar,50,birth),           //@MyBirthday Varchar(50),
                   SqlHelper.MakeInParam("@Email",SqlDbType.VarChar,50,""),              //@Email Varchar(50),
                   SqlHelper.MakeInParam("@RegSource",SqlDbType.SmallInt,4,int.Parse(regsource) ),             //@RegSource SmallInt,   19  v.9158.com网页注册
                   SqlHelper.MakeInParam("@Introducer",SqlDbType.VarChar,50,intro),       //@Introducer Varchar(50),
                   SqlHelper.MakeInParam("@RegisterIp",SqlDbType.VarChar,50,ip),           //@RegisterIp Varchar(50),
                   SqlHelper.MakeOutParam("@Return",SqlDbType.Int,4,stat)                //@Return Int Output
                };
            SqlHelper.ExecuteNonQuery(conn70, CommandType.StoredProcedure, "Register_Insert_Member", p);
            stat = int.Parse(p[12].Value.ToString());

        }
        catch (Exception)
        {
            // Common.Logs.WriteErrorLog(e.Message + "[注册失败70数据库：" + DateTime.Now.ToString() + "]");
        }
        return stat;
    }
    #endregion

    #region 判断用户名是否可用(注册)
    public static int ExisUid(string userid)
    {
        SqlParameter[] p ={
                SqlHelper.MakeInParam("@UserId",SqlDbType.VarChar,20,userid)       // @UserId Varchar(20),
            };
        object i = SqlHelper.ExecuteScalar(conn70, CommandType.StoredProcedure, "tenReg_CheckUserName", p);
        return int.Parse(i.ToString());
    }
    #endregion
    /// <summary>
    /// 获取地址 (城市)
    /// </summary>
    /// <param name="IP"></param>
    /// <returns></returns>
    #region
    public static DataRow[] GetAddress_r(string IP)
    {
        DataTable iptable = null;
        DataRow[] obj = null;
        long N_Ip = ConvertIpToMetric(IP);
        if (System.Web.HttpRuntime.Cache["allipcach"] == null)
        {
            try
            {
                iptable = SqlHelper.ExecuteDataTable(conn70, CommandType.StoredProcedure, "f_get_allip");
                System.Web.HttpRuntime.Cache.Insert("allipcach", iptable, null, DateTime.Now.AddDays(10), TimeSpan.Zero);
            }
            catch { return null; }
        }
        else
        {
            iptable = (DataTable)System.Web.HttpRuntime.Cache["allipcach"];
        }
        try
        {
            obj = iptable.Select(" IPFromINT<" + N_Ip + " and " + N_Ip + "<IPToINT");
        }
        catch { }
        return obj;
    }
    /// <summary>
    /// 根据IP获得当前省
    /// </summary>
    /// <param name="IP"></param>
    /// <returns></returns>
    public static string GetThePidByName(int IP)
    {
        string str_info = "";

        string sql = String.Format("select top 1 pro from Tb_Pro_City where pid ='{0}'", IP);

        try
        {
            str_info = SqlHelper.ExecuteScalar(conn70, CommandType.Text, sql).ToString();
        }
        catch (Exception)
        {
            str_info = "其他";
        }

        return str_info;
    }
    /// <summary>
    /// 获取省ID 根据ip    根据ip得到省,市
    /// </summary>
    /// <param name="IP"></param>
    /// <returns></returns>
    public static int GetThePidByIp(string IP)
    {
        int pid = -1;
        long N_Ip = ConvertIpToMetric(IP);
        DataTable iptable = null;
        if (System.Web.HttpRuntime.Cache["allipcach"] == null)
        {
            try
            {
                iptable = SqlHelper.ExecuteDataTable(conn70, CommandType.StoredProcedure, "f_get_allip");
                System.Web.HttpRuntime.Cache.Insert("allipcach", iptable, null, DateTime.Now.AddDays(5), TimeSpan.Zero);
            }
            catch { }
        }
        else
        {
            iptable = (DataTable)System.Web.HttpRuntime.Cache["allipcach"];
        }
        DataRow[] piprow = iptable.Select(N_Ip + " between IPFromINT and IPToINT");
        if (piprow.Length > 0)
        {
            pid = int.Parse(piprow[0]["pid"].ToString());
        }
        return pid;
    }
    /// <summary>
    /// IP格式 转换
    /// </summary>
    /// <param name="Ip"></param>
    /// <returns></returns>
    private static long ConvertIpToMetric(string Ip)
    {
        //取出IP地址去掉‘.’后的string数组
        string[] Ip_List = Ip.Split(".".ToCharArray());
        string X_Ip = "";
        //循环数组，把数据转换成十六进制数，并合并数组(3dafe81e)
        foreach (string ip in Ip_List)
        {
            X_Ip += Convert.ToInt16(ip).ToString("x");
        }
        //将十六进制数转换成十进制数(1034938398)
        long N_Ip = long.Parse(X_Ip, System.Globalization.NumberStyles.HexNumber);
        return N_Ip;
    }


    #endregion
}

